//
//  NewTagView.swift
//  MyDiary2025
//
//  Created by 罗万能 on 2025/6/28.
//


import SwiftUI

struct NewOrUpdateTagView: View {
    @Environment(\.dismiss) private var dismiss
    @Environment(\.modelContext) private var modelContext
    
    @State private var tagTitle: String = ""
    @State private var selectedColor: Color = .primary
    
    var tagForEdit: TagModel? = nil
    // 颜色选项
    let colorOptions: [Color] = [
        .red, .green, .blue, .yellow, .purple, .orange,
        .pink, .teal, .cyan, .indigo, .mint, .brown
    ]
    
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            // 标题
            Text("新建标签")
                .foregroundStyle(.title)
                .font(.title.bold())
                .padding()
            
            // 标签名称输入
            HStack{
                Text("#")
                    .foregroundStyle(selectedColor)
                TextField("输入标签名称", text: $tagTitle)
                    .foregroundStyle(selectedColor)
                
                    .textFieldStyle(.roundedBorder)
                
            }.padding()
                .font(.title2)
                .padding()
                .background(
                    RoundedRectangle(cornerRadius: 15)
                        .fill(.back)
                        .shadow(radius: 5)
                        .padding(.horizontal)
                )
            //颜色选择
            VStack(alignment: .leading, spacing: 20) {
                Text("选择颜色")
                    .font(.headline)
                    .padding(.leading, 20)
                
                ScrollView(.horizontal){
                    HStack{
                        ForEach(colorOptions, id: \.self) { color in
                            Circle()
                                .fill(color)
                                .frame(width: selectedColor == color ? 50 : 36, height: selectedColor == color ? 50 : 36)
                            
                                .overlay(
                                    
                                    Circle()
                                        .stroke(selectedColor == color ? Color.primary : .clear, lineWidth: 2)
                                )
                                .onTapGesture {
                                    withAnimation{
                                        selectedColor = color
                                    }
                                }
                        }
                    }
                }
                .padding()
                .scrollIndicators(.hidden)
            }
            .padding()
            .background(
                RoundedRectangle(cornerRadius: 15)
                    .fill(Color("back"))
                    .shadow(radius: 5)
                    .padding(.horizontal)
            )
            //
            Spacer()
            
            // 保存按钮
            Button("创建标签") {
                saveTag()
            }
            .font(.title3.bold())
            .foregroundColor(.title)
            .padding()
            .frame(maxWidth: .infinity)
            .background(
                Capsule()
                    .fill(Color("back"))
                    .shadow(radius: 10)
            )
            .padding(.horizontal)
            .disabled(tagTitle.isEmpty)
            Button("取消") {
                cancle()
            }
            .font(.title3.bold())
            .foregroundColor(.red)
            .padding()
            .frame(maxWidth: .infinity)
            .background(
                Capsule()
                    .fill(Color("back"))
                    .shadow(radius: 10)
            )
            .padding(.horizontal)
            .disabled(tagTitle.isEmpty)
        }
        //            .navigationBarHidden(true)
        .background(Color("back"))
        //            .edgesIgnoringSafeArea(.all)
        .onAppear {
            if let tag = tagForEdit{
                self.tagTitle = tag.tagTitle
                self.selectedColor = tag.tagColor
            }
        }
    }
    
    
    // 保存标签
    private func saveTag() {
        if let tag = tagForEdit{
            tag.tagTitle = self.tagTitle
            tag.tagColor = self.selectedColor
        }else{
            let newTag = TagModel(
                id: UUID(),
                tagTitle: tagTitle,
                tagColor: selectedColor
            )
            
            modelContext.insert(newTag)
        }
        do{
            try modelContext.save()
        }catch{
            print(error)
        }
        dismiss()
    }
    private func cancle(){
        self.tagTitle = ""
        self.selectedColor = Color.primary
        dismiss()
    }
}

#Preview {
    NewOrUpdateTagView()
}
